﻿#include "CommonFunction.h"

namespace pst
{

    // 字符串分割
    CommonFunction::CommonFunction()
    {

    }

    void CommonFunction::Stringsplit(const std::string& str, const std::string& splits,
        std::vector<std::string>& res)
    {
        if (str == "")
        {
            return;
        }
        //在字符串末尾也加入分隔符，方便截取最后一段
        std::string strs = str + splits;
        size_t pos = strs.find(splits);
        int step = splits.size();
        res.clear();
        // 若找不到内容则字符串搜索函数返回 npos
        while (pos != strs.npos)
        {
            std::string temp = strs.substr(0, pos);
            res.push_back(temp);
            //去掉已分割的字符串,在剩下的字符串中进行分割
            strs = strs.substr(pos + step, strs.size());
            pos = strs.find(splits);
        }
    }

    //去除字符串首尾空格
    std::string CommonFunction::trim(const std::string& s1)
    {
        std::string s = s1;
        if (!s.empty())
        {
            s.erase(0, s.find_first_not_of(" "));
            s.erase(s.find_last_not_of(" ") + 1);
        }
        return s;
    }

    void CommonFunction::SphereToRectangularCoorSys(const double theta, const double phi, const double r, double& x, double& y, double& z)
    {
        double my_pi = 3.141592653589793238462643383279502884L;
        double val = my_pi / 180;

        x = r * std::sin(theta * val) * std::cos(phi * val);
        y = r * std::sin(theta * val) * std::sin(phi * val);
        z = r * std::cos(theta * val);
    }

    void CommonFunction::PolarToRectangularCoorSys(const double theta, const double r, double& x, double& y)
    {
        double my_pi = 3.141592653589793238462643383279502884L;
        double val = my_pi / 180;
        x = r * std::cos(theta * val);
        y = r * std::sin(theta * val);
    }

}

